package com.neusoft.liduan.d0915;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;

import javax.swing.JPanel;

public class CanvasPanel extends JPanel {
	private String type = "l";
//	private Shape[] shapes;
//	private int shapeIndex;//数组的索引
	private ShapeArray shapeArray;

	public void setType(String type) {
		this.type = type;
	}

	public CanvasPanel() {
		// 添加点击事件
		// MouseAdapter属于适配器开发模式
		addMouseMotionListener(ma);
		addMouseListener(ma);
//		shapes = new Shape[10];
		shapeArray = new ShapeArray();
	}
	
//	private int x1;
//	private int y1;
//	private int x2;
//	private int y2;
//	private int w;
//	private int h;
//	
//	private Line line;
//	private Square square;
	private Shape shape;
	
//	private int getShapeIndex() {
//		int index = 0;
//		int len = shapes.length;
//		if(len == 0) {
//			return index;
//		}
//		for(int i = 0; i < shapes.length; i++) {
//			if(shapes[i] == null) {
//				index = i;
//			}
//		}
//		
//		return index;
//	}
	
	private MouseAdapter ma = new MouseAdapter() {
		public void mousePressed(MouseEvent e) {
			System.out.println("mousePressed");
			//获取点击坐标
//			x1 = e.getX();
//			y1 = e.getY();
			if("l".equals(type)) {
//				line = new Line();
//				line.setX1(x1);
//				line.setY1(y1);
				shape = new Line();
			}else if("s".equals(type)) {
//				square = new Square();
//				square.setX1(x1);
//				square.setY1(y1);
				shape = new Square();
			}
			shape.setX1(e.getX());
			shape.setY1(e.getY());
//			shapeIndex = getShapeIndex();
//			isPaint = true;
		}

		public void mouseReleased(MouseEvent e) {
			System.out.println("mouseReleased");
//			x2 = e.getX();
//			y2 = e.getY();
//			line.setX2(x2);
//			line.setY2(y2);
//			square.setX2(x2);
//			square.setY2(y2);
			shape.setX2(e.getX());
			shape.setY2(e.getY());
//			shapes[shapeIndex] = shape;
			shapeArray.push(shape);
			shape = null;
			repaint();
		}

		public void mouseDragged(MouseEvent e) {
			System.out.println("mouseDragged");
//			x2 = e.getX();
//			y2 = e.getY();
//			line.setX2(x2);
//			line.setY2(y2);
//			square.setX2(x2);
//			square.setY2(y2);
//			w = x2 - x1;
//			h = y2 - y1;
			shape.setX2(e.getX());
			shape.setY2(e.getY());
			repaint();
		}
	};
	
//	private boolean isPaint = false;

	@Override
	public void paint(Graphics g) {
		//清除历史记录
		/*	1、怎么画矩形
		 * 	2、如何封装类来画直线和矩形
		 * 	3、保存历史记录
		 * 	4、封装一个类，保存所有图形，基于数组保存
		 * 		1、当数组饱和时，创建一个更长的数组
		 * 		2、提供添加图形方法，效果类似于JS中的push方法
		 * 		3、提供清空所有图形的方法，empty()
		 */
		super.paint(g);//执行了父类中方法的代码
		System.out.println("paint执行了");
//		if(isPaint) {
//			if ("l".equals(type)) {
//				// 画直线
////				g.drawLine(x1, y1, x2, y2);
//				line.draw(g);
//			} else if ("s".equals(type)) {
//				// 画矩形
////				g.drawRect(x1, y1, w, h);
//				square.draw(g);
//			} else if ("c".equals(type)) {
//				g.drawOval(x1, y1, w, h);
//			}
//		}
		if(shape != null) {
//			System.out.println(1);
			shape.draw(g);
		}
//		for(int i = 0; i < shapeArray.getArray().length; i++) {
//			if(shapeArray.getArray()[i] == null) {
//				break;
//			}
//			shapeArray.getArray()[i].draw(g);
//		}
		shapeArray.draw(g);
	}

}